
import pyglet
import random

# Set Size of Window
WinWidth = 640
WinHeight = 480
window = pyglet.window.Window(WinWidth, WinHeight)
TXTbatch = pyglet.graphics.Batch()

background = pyglet.graphics.OrderedGroup(0)
foreground = pyglet.graphics.OrderedGroup(1)

# Create text label
label = pyglet.text.Label('Happy New Year!', 
                          font_name='Arial', 
                          font_size=int(WinHeight/10),
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center',
                          batch=TXTbatch, group=foreground,
                          color = (235,0,0,255))

class star(object):
    def __init__(self):
        self.X = 0
        self.Y = 0
        self.Z = 0
        self.SetRandomPosition()
    def SetRandomPosition(self):
        self.X = random.randrange(-20, 30)
        self.Y = random.randrange(-20, 30)
        self.Z = random.randrange(20, 50)

def AddXYZ( batch, x, y, z, c):
    global WinHeight
    global WinWidth
    fov = 128
    viewdistance = 3
    
    x = x * fov / (z + viewdistance) + (WinHeight/2)
    y = -y * fov / (z + viewdistance) + (WinWidth/2)
    
    vertex_list = batch.add(1, pyglet.gl.GL_POINTS, None,
            ('v2i', (x,y)),
            ('c3B', (252-z*4, 252-z*4, 252-z*4)))

@window.event
def on_draw():
    global batch
    TXTbatch.draw()
        
def update(ts):
    global sf
    window.clear()
    for l in sf:
        l.Z -= 1
        if l.Z<-2:
            l.Z = random.randrange(20,30)
    b = CreateBatch(sf)
    b.draw()
    TXTbatch.draw()
    
pyglet.clock.schedule_interval(update,  0.01)

sf = []
for l in range(40):
    sf.append(star())

def CreateBatch(sf):
    batch = pyglet.graphics.Batch()
    for star in sf:
        AddXYZ(batch, star.X, star.Y, star.Z, (255,255,255))
    return batch        

pyglet.app.run()
